package com.anolesoft.Planexecution.fcfhprint.controller;

import com.anolesoft.Planexecution.fcfhprint.dto.PrintfinfoDto;
import com.anolesoft.Planexecution.fcfhprint.service.PrintfinfoService;
import com.anolesoft.Planexecution.fcfhprint.service.mapper.PrintfinfoMapper;
import com.anolesoft.common.core.domain.LoginUser;
import com.anolesoft.common.core.web.domain.AjaxResult;
import com.anolesoft.common.security.util.SecurityUtils;
import com.anolesoft.yr.demo.PageResult;
import com.anolesoft.yr.util.Constants;
import com.anolesoft.yr.util.HttpStatus;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;
import org.springframework.web.bind.annotation.*;

import javax.annotation.Resource;
import java.util.List;

@RestController
@RequestMapping(value = "/mesbzzprint")
public class PrintfinfoController {

    @Autowired
    private PrintfinfoService service;
    @Resource
    private PrintfinfoMapper mapper;

    @RequestMapping(value = "/query" , method = RequestMethod.POST)
    public AjaxResult queryMesBzzDegsInfo(@RequestBody PrintfinfoDto printfinfoDto){
        LoginUser loginUser = com.anolesoft.yr.util.SecurityUtils.getLoginUser();
        //厂代号
        String fcode = loginUser.getFacCode();
        printfinfoDto.setFactoryId(fcode);
        int totalcount = service.counttiao(printfinfoDto);
        Integer pageNum = printfinfoDto.getPageNo();
        Integer pageSize = printfinfoDto.getPageSize();
        Integer pageBug = pageNum * pageSize > totalcount ? totalcount : pageNum * pageSize;
        Integer pageSmall = (pageNum - 1) * pageSize;
        printfinfoDto.setPageBug(pageBug);
        printfinfoDto.setPageSmall(pageSmall);
        List<PrintfinfoDto> allList =  service.listall(printfinfoDto);
//        int endIndex = pageNum * pageSize > allList.size() ? allList.size() : pageNum * pageSize;
//        List<PrintfinfoDto> list = allList.subList( (pageNum-1) *pageSize, endIndex);
        PageResult pageResult = new PageResult(allList, pageSize, pageNum, totalcount);
        return new AjaxResult(HttpStatus.SUCCESS, Constants.QUERY_SUCCESS_MESSAGE,pageResult);
    }


    @RequestMapping(value = "/print" , method = RequestMethod.POST)
    @ResponseBody
    @Transactional
    public AjaxResult weiHuMesGjgyxxFkszInfoDto(@RequestBody PrintfinfoDto printfinfoDto){
        List<String> list = printfinfoDto.getList();
        LoginUser loginUser = SecurityUtils.getLoginUser();
        int b = 0;
        for (String item:
                list) {
            Integer a = mapper.countprint(item);
            if (a == 0) {
                b = mapper.updateprintzero(loginUser.getUserUid(),item);
            } else {
                b = mapper.updateprint(loginUser.getUserUid(),item);
            }
        }
        return new AjaxResult(com.anolesoft.common.core.constant.HttpStatus.SUCCESS, "维护成功!", b);
    }

}
